// pages/website.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    companyList:[],
    productList: [],
    Goods: [],
    Videos: [],
    ContactList: [],
    Persons: [],
    contactInfo: {},
    video_id:'',
    imgH: '',
    current:0,
    current1:0,
    imgH1: '',
  },
  // 视频播放,暂停上一个视频
  startPlayVideo: function (e) {
    // 本视频id
    var id = e.currentTarget.id;
    // 上个一视频id
    var prev_id = this.data.video_id;
    // 停止山一个视频播放
    if (id != prev_id) {
      wx.createVideoContext(prev_id).pause();
      this.setData({
        video_id: id
      })
    }
  },

  imageLoad: function (e) {
    //获取图片真实宽度
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比
      ratio = imgwidth / imgheight;
    // console.log(imgwidth, imgheight)
    //计算的高度值
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight
    var imgheights = []
    //把每一张图片的高度记录到数组里
    imgheights.push(imgheight)
    var imgH = Math.max.apply(null, imgheights)
    console.log(imgH)
    this.setData({
      imgH: imgH,
    })
  },
  bindchange: function (e) {
    // console.log(e.detail.current)
    this.setData({ current: e.detail.current })
  },
  imageLoad1: function (e) {
    //获取图片真实宽度
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比
      ratio = imgwidth / imgheight;
    // console.log(imgwidth, imgheight)
    //计算的高度值
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight
    var imgheights1 = []
    //把每一张图片的高度记录到数组里
    imgheights1.push(imgheight)
    var imgH1 = Math.max.apply(null, imgheights1)
    console.log(imgH1)
    this.setData({
      imgH1: imgH1,
    })
  },
  bindchange1: function (e) {
    // console.log(e.detail.current)
    this.setData({ current1: e.detail.current })
  },
  // 点击更多进去公司视频
  bindComVideos: function (e) {
    wx.navigateTo({
      url: '../companyVideos/companyVideos',
    })
  },
  // 点击更多进去企业资讯
  bindComNews: function (e) {
    wx.navigateTo({
      url: '../companyNews/companyNews',
    })
  },
  // 点击企业资讯进入资讯详情
  bindComNewsDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../companyNewsDetail/companyNewsDetail?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: app.globalData.userInfo,
      systemInfo: app.globalData.systemInfo,
      u_id: app.globalData.userInfo.id,
      v_id: app.globalData.SwitchTabParams.v_id
    })
  },

  //公司视频
  getVideoList: function () {
    var that = this;
    var url = '/platform_api/news';
    var params = {
      uid: app.globalData.SwitchTabParams.v_id,
      type: 1,
      pageno: 1,
      pagesize:4
    };
    app.HttpPostSend(url, params, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        that.setData({
          Videos: res.data
        })
      }
    });
  },

  //公司资讯,
  getGoodsList: function () {
    var that = this;
    var url = '/platform_api/news';
    var params = {
      uid: app.globalData.SwitchTabParams.v_id,
      type: 2,
      pageno: 1,
      pagesize: 4
    };
    app.HttpPostSend(url, params, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        var goods = res.data;
        for(var i=0;i<goods.length;i++){
          goods[i].create_time = that.timestampToTime(goods[i].create_time * 1000);
        }
        that.setData({
          Goods: goods
        })
      }
    });
  },

  //合作伙伴
  getContactList: function () {
    var that = this;
    var url = '/platform_api/news';
    var params = {
      uid: app.globalData.SwitchTabParams.v_id,
      type: 3,
      pageno: 1,
      pagesize: 4
    };
    app.HttpPostSend(url, params, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        that.setData({
          ContactList: res.data
        })
      }
    });
  },

  //公司成员
  getPersonsList: function () {
    var that = this;
    var url = '/platform_api/members';
    var params = {
      uid: app.globalData.SwitchTabParams.v_id,
    };
    app.HttpPostSend(url, params, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        that.setData({
          Persons: res.data
        })
      }
    });
  },

  //公司信息
  getcontactInfo: function () {
    var that = this;
    var url = '/platform_api/information';
    var params = {
      uid: app.globalData.SwitchTabParams.v_id,
    };
    app.HttpPostSend(url, params, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        that.setData({
          contactInfo: res.data
        })
      }
    });
  },

  timestampToTime: function (timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '年';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + '日';
    var h = date.getHours() + ':';
    if (String(date.getMinutes()).length == 1) {
      var m = "0" + date.getMinutes() + ':';
    } else {
      m = date.getMinutes() + ':';
    }
    if (String(date.getSeconds()).length == 1) {
      var s = "0" + date.getSeconds() + ':';
    } else {
      s = date.getSeconds();
    }
    return Y + M + D;
  },//转化后格式xxxx-xx-xx,

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userInfo: app.globalData.userInfo,
      systemInfo: app.globalData.systemInfo,
      u_id: app.globalData.userInfo.id,
      v_id: app.globalData.SwitchTabParams.v_id
    })
     
    this.getVideoList(); 
    this.getGoodsList(); 
    this.getContactList(); 
    this.getPersonsList();
    this.getcontactInfo();

    var that = this;
    var url = '/platform_api/allBanners';
    var params = {
      uid: app.globalData.SwitchTabParams.v_id
    };
    app.HttpPostSend(url, params, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        var bannerList = res.data.one;
        var companyList = res.data.two;
        var productList = res.data.three;
        that.setData({
          bannerList: bannerList,
          companyList: companyList,
          productList: productList
        })
      }
    });
    

    try {
      // ai添加纪录 对公司感兴趣
      app.aiAddRecord(3)
    } catch (error) {
      console.log(error)
    }

    if (app.globalData.userInfo.id != app.globalData.SwitchTabParams.v_id) {

      // 雷达添加查看记录 -官网访问数
      app.radarAdd(app.globalData.SwitchTabParams.v_id, 7, '', '', app.globalData.userInfo.logo)

      // 发送模板消息-访客到访通知
      app.sendTemplateMessage(app.globalData.SwitchTabParams.v_id, 1, '/radarPages/radarCenter/radarCenter?coming_status=1', '正在访问官网', '', '', '')
    }
  },

  // 打开个人统计信息页
  showPerson: function () {
    wx.switchTab({
      url: '/pages/person/person',
    })
  },
  

  // wx.makePhoneCall({
  makeCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.content// 仅为示例，并非真实的电话号码
    })
  },

  // 复制到剪贴板
  copyToCli: function (e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.content,
      success(res) {
        app.showTipMsg('复制到剪贴板')
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  renderCarte: function (params) {
    const that = this
    // let 
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})